package webapp.frontOffice.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modele.contrat.Action;
import modele.contrat.OptionC;
import modele.utilisateur.Entreprise;
import modele.utilisateur.MembreSociete;
import dao.contrat.ActionDAO;
import dao.contrat.OptionCDAO;
import dao.utilisateur.EntrepriseDAO;

/**
 * Servlet implementation class MaSociete
 */
@WebServlet("/masociete")
public class MaSociete extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		String action = request.getParameter("action");
		
		if(session == null || session.getAttribute("type") == null || !session.getAttribute("type").equals("membre"))
		{
			response.sendRedirect("index.jsp");
		}


		else 
		{
			Entreprise societe = ((MembreSociete)session.getAttribute("user")).getEntreprise();
			request.setAttribute("societe", societe);
			request.setAttribute("membre", true);
			List<Action> actions = new ActionDAO().retrieveByEntreprise(societe);
			List<OptionC> options = new OptionCDAO().retrieveByEntreprise(societe);
			request.setAttribute("actions", actions);
			request.setAttribute("options", options);
			
			if(action == null)
			{
				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/viewFrontoffice/societe.jsp");
				dispatcher.forward(request, response);
			}
			else if(action.equals("edit"))
			{

				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/viewFrontoffice/societeEdit.jsp");
				dispatcher.forward(request, response);
			}
		}
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Entreprise societe = ((MembreSociete)request.getSession().getAttribute("user")).getEntreprise();
		
		societe.setType(request.getParameter("type"));
		societe.setSecteur(request.getParameter("secteur"));
		societe.setCapitalisation(Double.parseDouble(request.getParameter("capitalisation")));
		societe.setDescription(request.getParameter("description"));
		societe.setPER(Double.parseDouble(request.getParameter("per")));
		societe.setBPA(Double.parseDouble(request.getParameter("bpa")));
		
		new EntrepriseDAO().update(societe);
		
		response.sendRedirect("masociete");
	}

}
